﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using LiJu.Common;

namespace LiJu
{
    /// <summary>
    /// add zhl 主要功能：ddk电批参数读取、写入，并更新到数据库
    /// </summary>
    public partial class ParameterManager3 : Form
    {
        /// <summary>
        /// 读取ddk参数集合
        /// </summary>
        public List<ScrewParam> ReadScrewParams = new List<ScrewParam>();

        private Image torqueParmImg = Properties.Resources.parameterSet1;
        private Image torqueLimtParmImg = Properties.Resources.parameterSet2;
        private Image speedParmImg = Properties.Resources.parameterSet3;
        public ParameterManager3()
        {
            InitializeComponent();
            InitGridView();
            InitImageNavigation();
        }

        private void ParameterManager3_Load(object sender, EventArgs e)
        {
            this.dgvParameter.Columns["ToolTypeColumn"].ReadOnly = true;
            this.Enabled = false;
            this.label1.Visible = true;
            this.panel1.Visible = true;
            this.progressBar1.Visible = true;
            this.progressBar1.Value = 0;
            DDKBatch.ScrewParamReturnAction += AddScrewParameters;
            DDKBatch.isFirst = false;
            this.backgroundWorker1.RunWorkerAsync();
        }
        /// <summary>
        /// 初始化表格数据
        /// </summary>
        private void InitGridView()
        {
            dgvParamName.AllowUserToAddRows = false;
            dgvParameter.AllowUserToDeleteRows = false;

            dgvParamName.CellBeginEdit += dgvParamName_CellBeginEdit;
            dgvParamName.CellEndEdit += dgvParamName_CellEndEdit;
            dgvParameter.CellBeginEdit += dgvParameter_CellBeginEdit;
            dgvParameter.CellEndEdit += dgvParameter_CellEndEdit;

            //列标题居中
            dgvParamName.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //单元格字体居中显示
            dgvParamName.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            // 设置选中单元格的背景颜色
            dgvParamName.DefaultCellStyle.SelectionBackColor = System.Drawing.ColorTranslator.FromHtml("#BFEFFF");
            //设置选择模式为整行选择
            dgvParamName.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            // 设置选中单元格的前景颜色
            dgvParamName.DefaultCellStyle.SelectionForeColor = Color.Black;

            // 设置选择模式
            dgvParamName.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvParamName.MultiSelect = false;

            // 设置选中单元格的背景颜色
            dgvParameter.DefaultCellStyle.SelectionBackColor = System.Drawing.ColorTranslator.FromHtml("#BFEFFF");
            //设置选择模式为整行选择
            //parameterDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            // 设置选中单元格的前景颜色
            dgvParameter.DefaultCellStyle.SelectionForeColor = Color.Black;
            //this.parameterDataGridView.DefaultCellStyle.Font = defaultFont;
            // 创建一个新的Font对象
            Font columnFont = new Font("Microsoft YaHei", 10, FontStyle.Bold);
            // 设置列头的默认单元格样式
            dgvParamName.ColumnHeadersDefaultCellStyle.Font = columnFont;
            dgvParameter.ColumnHeadersDefaultCellStyle.Font = columnFont;

        }
        string oldvalue = null;
        /// <summary>
        /// 参数修改开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvParameter_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }
            oldvalue = dgvParameter.Rows[e.RowIndex].Cells[e.ColumnIndex].Value?.ToString();
        }

        /// <summary>
        /// 参数修改结束
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvParameter_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string newvalue = dgvParameter.Rows[e.RowIndex].Cells[e.ColumnIndex].Value?.ToString();
            if (string.IsNullOrEmpty(newvalue)) return;
            if (e.ColumnIndex > 1)
            {
                if (!double.TryParse(newvalue, out double value))
                {
                    MessageBox.Show("请输入有效数");
                    dgvParameter.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = oldvalue;
                }
                else
                {
                    if (value < 0)
                    {
                        MessageBox.Show("值不能小于0");
                        dgvParameter.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = oldvalue;
                        return;
                    }
                    if (newvalue.Trim() != oldvalue)
                    {
                        DialogResult dialogResult = MessageBox.Show("您是否确认修改？", "确认修改", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (dialogResult == DialogResult.Yes)
                        {
                            //确定修改，保存新值
                            dgvParameter.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = newvalue.Trim();
                        }
                        else
                        {
                            //取消选择，则撤销修改
                            dgvParameter.CancelEdit();
                            dgvParameter.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = oldvalue;
                        }
                    }
                    UpdateParamData(e.ColumnIndex, e.RowIndex);
                }
            }
            else if (e.ColumnIndex == 1)
            {
                if (!string.IsNullOrEmpty(newvalue))
                {
                    UpdateParamData(e.ColumnIndex, e.RowIndex);
                }
            }
        }
        string oldName = null;
        /// <summary>
        /// 参数名修改前
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvParamName_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }
            oldName = dgvParamName.Rows[e.RowIndex].Cells[e.ColumnIndex].Value?.ToString();
        }
        /// <summary>
        /// 参数名修改结束
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvParamName_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string newName = dgvParamName.Rows[e.RowIndex].Cells[e.ColumnIndex].Value?.ToString();
            if (e.ColumnIndex == 1)
            {
                if (string.IsNullOrEmpty(newName))
                {
                    MessageBox.Show("请输入参数名称");
                    dgvParamName.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = oldvalue;
                }
                else
                {
                    int index = (int)dgvParamName.Rows[e.RowIndex].Cells["ParamIndexColumn"].Value;
                    var param = ReadScrewParams.Find(o => o.taskId == index);
                    if (param != null)
                    {
                        param.name = newName;
                    }
                    dgvParamName.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = newName;
                }
            }
        }
        /// <summary>
        /// 更新参数
        /// </summary>
        private void UpdateParamData(int colIndex, int rowIndex)
        {
            int index = (int)dgvParamName.Rows[dgvParamName.CurrentRow.Index].Cells["ParamIndexColumn"].Value;
            ScrewParam screwParam = ReadScrewParams.Find(o => o.taskId == index);
            string colName = dgvParameter.Columns[colIndex].Name;
            string rowValue = dgvParameter.Rows[rowIndex].Cells[colIndex].Value.ToString();
            double value = 0;
            if (colName == "TighteningTypeColumn")
            {
                screwParam.tighteningType = rowValue;

                //扭矩法
                if (rowValue == DDKBatch.TighteningTypeList[0])
                {
                    dgvParameter.Columns["TargetTorqueColumn"].Visible = true;
                    dgvParameter.Columns["MaxTorqueDownLimitColumn"].Visible = true;
                    dgvParameter.Columns["MaxTorqueUpLimitColumn"].Visible = true;
                    dgvParameter.Columns["TargetAngleColumn"].Visible = false;
                    //dgvParameter.Columns["LastAngleDownlimitColumn"].Visible = false;
                    //dgvParameter.Columns["LastAngleUplimitColumn"].Visible = false;
                    dgvParameter.Columns["LastAngleUplimitColumn"].DisplayIndex = dgvParameter.Columns["FisrtTorqueColumn"].DisplayIndex - 1;
                    dgvParameter.Columns["LastAngleDownlimitColumn"].DisplayIndex = dgvParameter.Columns["FisrtTorqueColumn"].DisplayIndex - 1;
                }
                //角度法
                else if (rowValue == DDKBatch.TighteningTypeList[1])
                {
                    dgvParameter.Columns["TargetTorqueColumn"].Visible = false;
                    //dgvParameter.Columns["MaxTorqueDownLimitColumn"].Visible = false;
                    //dgvParameter.Columns["MaxTorqueUpLimitColumn"].Visible = false;
                    dgvParameter.Columns["MaxTorqueUpLimitColumn"].DisplayIndex = dgvParameter.Columns["FisrtTorqueColumn"].DisplayIndex - 1;
                    dgvParameter.Columns["MaxTorqueDownLimitColumn"].DisplayIndex = dgvParameter.Columns["FisrtTorqueColumn"].DisplayIndex - 1;
                    dgvParameter.Columns["TargetAngleColumn"].Visible = true;
                    dgvParameter.Columns["LastAngleDownlimitColumn"].Visible = true;
                    dgvParameter.Columns["LastAngleUplimitColumn"].Visible = true;
                }
                dgvParameter.Rows[rowIndex].Cells[colIndex].Value = rowValue;
            }
            else if (colName == "TargetTorqueColumn" && double.TryParse(rowValue, out value))
            {
                if (value > screwParam.allRangeTorque)
                {
                    MessageBox.Show("目标扭矩不能大于校准力矩");
                    dgvParameter.Rows[rowIndex].Cells[colIndex].Value = oldvalue;
                    return;
                }
                screwParam.targetTorque = value;
            }
            else if (colName == "TargetAngleColumn" && double.TryParse(rowValue, out value))
            {
                if (value >= 10000)
                {
                    MessageBox.Show("目标角度不能大于9999.9");
                    dgvParameter.Rows[rowIndex].Cells[colIndex].Value = oldvalue;
                    return;
                }
                screwParam.targeAngle = value;
            }
            else if (colName == "MaxTorqueDownLimitColumn" && double.TryParse(rowValue, out value))
            {
                if (value > screwParam.allRangeTorque)
                {
                    MessageBox.Show("最大扭矩下限不能大于校准力矩");
                    dgvParameter.Rows[rowIndex].Cells[colIndex].Value = oldvalue;
                    return;
                }
                screwParam.maxTorqueLowLimit = value;
            }
            else if (colName == "MaxTorqueUpLimitColumn" && double.TryParse(rowValue, out value))
            {
                if (value > screwParam.allRangeTorque)
                {
                    MessageBox.Show("最大扭矩上限不能大于校准力矩");
                    dgvParameter.Rows[rowIndex].Cells[colIndex].Value = oldvalue;
                    return;
                }
                screwParam.maxTorqueUpLimit = value;
            }
            else if (colName == "FisrtTorqueColumn" && double.TryParse(rowValue, out value))
            {
                if (value > screwParam.targetTorque && screwParam.tighteningType == DDKBatch.TighteningTypeList[0])
                {
                    MessageBox.Show("1ST扭矩不能大于目标扭矩");
                    dgvParameter.Rows[rowIndex].Cells[colIndex].Value = oldvalue;
                    return;
                }
                screwParam.firstTorque = value;
            }
            else if (colName == "SungTorqueColumn" && double.TryParse(rowValue, out value))
            {
                if (value > screwParam.targetTorque && screwParam.tighteningType == DDKBatch.TighteningTypeList[0])
                {
                    MessageBox.Show("SUNG扭矩不能大于目标扭矩");
                    dgvParameter.Rows[rowIndex].Cells[colIndex].Value = oldvalue;
                    return;
                }
                screwParam.sungTorque = value;
            }
            else if (colName == "SecondTorqueColumn" && double.TryParse(rowValue, out value))
            {
                if (value > screwParam.targetTorque && screwParam.tighteningType == DDKBatch.TighteningTypeList[0])
                {
                    MessageBox.Show("2ND扭矩不能大于目标扭矩");
                    dgvParameter.Rows[rowIndex].Cells[colIndex].Value = oldvalue;
                    return;
                }
                screwParam.secendTorque = value;
            }
            else if (colName == "FirstTimeUpLimitColumn" && double.TryParse(rowValue, out value))
            {
                screwParam.firstTimeUpLimit = value;
            }
            else if (colName == "SecondTimeUpLimitColumn" && double.TryParse(rowValue, out value))
            {
                screwParam.secendTimeUpLimit = value;
            }
            else if (colName == "FirstSpeedColumn" && double.TryParse(rowValue, out value))
            {
                if (value > 3000)
                {
                    MessageBox.Show("1st速度不能大于3000");
                    dgvParameter.Rows[rowIndex].Cells[colIndex].Value = oldvalue;
                    return;
                }
                screwParam.firstSpeed = value;
            }
            else if (colName == "SecondSpeedColumn" && double.TryParse(rowValue, out value))
            {
                if (value > 3000)
                {
                    MessageBox.Show("2nd速度不能大于3000");
                    dgvParameter.Rows[rowIndex].Cells[colIndex].Value = oldvalue;
                    return;
                }
                screwParam.secendSpeed = value;
            }
            else if (colName == "LastAngleDownLimitColumn" && double.TryParse(rowValue, out value))
            {
                if (value >= 10000)
                {
                    MessageBox.Show("最終角度下限不能大于9999.9");
                    dgvParameter.Rows[rowIndex].Cells[colIndex].Value = oldvalue;
                    return;
                }
                screwParam.lastAngleLowlimit = value;
            }
            else if (colName == "LastAngleUpLimitColumn" && double.TryParse(rowValue, out value))
            {
                if (value >= 10000)
                {
                    MessageBox.Show("最終角度上限不能大于9999.9");
                    dgvParameter.Rows[rowIndex].Cells[colIndex].Value = oldvalue;
                    return;
                }
                screwParam.lastAngleUplimit = value;
            }
        }
        /// <summary>
        /// 切换参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvParamName_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }
            int num = (int)dgvParamName.Rows[e.RowIndex].Cells["ParamIndexColumn"].Value;
            var param = ReadScrewParams.Find(o => o.taskId == num);
            dgvParameter.Rows.Clear();
            if (param != null)
            {
                SelectParam(num);
            }
            else
            {
                dgvParameter.Rows.Add();
            }
        }
        /// <summary>
        /// 选择参数
        /// </summary>
        private void SelectParam(int index)
        {
            dgvParameter.Rows.Clear();
            var param = ReadScrewParams.Find(o => o.taskId == index);
            if (param != null)
            {
                (dgvParameter.Columns["TighteningTypeColumn"] as DataGridViewComboBoxColumn).DataSource = DDKBatch.TighteningTypeList;
                (dgvParameter.Columns["ToolTypeColumn"] as DataGridViewComboBoxColumn).DataSource = DDKBatch.ToolTypeList;
                dgvParameter.Rows.Add();
                dgvParameter.Rows[0].Cells["ToolTypeColumn"].Value = param.ToolType; ;
                dgvParameter.Rows[0].Cells["TighteningTypeColumn"].Value = param.tighteningType;
                dgvParameter.Rows[0].Cells["TargetTorqueColumn"].Value = param.targetTorque;
                dgvParameter.Rows[0].Cells["TargetAngleColumn"].Value = param.targeAngle;
                //扭矩法
                if (param.tighteningType == DDKBatch.TighteningTypeList[0])
                {
                    dgvParameter.Columns["TargetTorqueColumn"].Visible = true;
                    dgvParameter.Columns["MaxTorqueDownLimitColumn"].Visible = true;
                    dgvParameter.Columns["MaxTorqueUpLimitColumn"].Visible = true;
                    dgvParameter.Columns["TargetAngleColumn"].Visible = false;
                    //dgvParameter.Columns["LastAngleDownlimitColumn"].Visible = false;
                    //dgvParameter.Columns["LastAngleUplimitColumn"].Visible = false;
                    dgvParameter.Columns["LastAngleUplimitColumn"].DisplayIndex = dgvParameter.Columns["FisrtTorqueColumn"].DisplayIndex - 1;
                    dgvParameter.Columns["LastAngleDownlimitColumn"].DisplayIndex = dgvParameter.Columns["FisrtTorqueColumn"].DisplayIndex - 1;
                }
                //角度法
                else if (param.tighteningType == DDKBatch.TighteningTypeList[1])
                {
                    dgvParameter.Columns["TargetTorqueColumn"].Visible = false;
                    //dgvParameter.Columns["MaxTorqueDownLimitColumn"].Visible = false;
                    //dgvParameter.Columns["MaxTorqueUpLimitColumn"].Visible = false;
                    dgvParameter.Columns["MaxTorqueUpLimitColumn"].DisplayIndex = dgvParameter.Columns["FisrtTorqueColumn"].DisplayIndex - 1;
                    dgvParameter.Columns["MaxTorqueDownLimitColumn"].DisplayIndex = dgvParameter.Columns["FisrtTorqueColumn"].DisplayIndex - 1;
                    dgvParameter.Columns["TargetAngleColumn"].Visible = true;
                    dgvParameter.Columns["LastAngleDownlimitColumn"].Visible = true;
                    dgvParameter.Columns["LastAngleUplimitColumn"].Visible = true;

                }
                dgvParameter.Rows[0].Cells["MaxTorqueDownLimitColumn"].Value = param.maxTorqueLowLimit;
                dgvParameter.Rows[0].Cells["MaxTorqueUpLimitColumn"].Value = param.maxTorqueUpLimit;
                dgvParameter.Rows[0].Cells["FisrtTorqueColumn"].Value = param.firstTorque;
                dgvParameter.Rows[0].Cells["SungTorqueColumn"].Value = param.sungTorque;
                dgvParameter.Rows[0].Cells["SecondTorqueColumn"].Value = param.secendTorque;
                dgvParameter.Rows[0].Cells["FirstTimeUpLimitColumn"].Value = param.firstTimeUpLimit;
                dgvParameter.Rows[0].Cells["SecondTimeUpLimitColumn"].Value = param.secendTimeUpLimit;
                dgvParameter.Rows[0].Cells["FirstSpeedColumn"].Value = param.firstSpeed;
                dgvParameter.Rows[0].Cells["SecondSpeedColumn"].Value = param.secendSpeed;
                dgvParameter.Rows[0].Cells["LastAngleDownlimitColumn"].Value = param.lastAngleLowlimit;
                dgvParameter.Rows[0].Cells["LastAngleUplimitColumn"].Value = param.lastAngleUplimit;
            }
        }
        /// <summary>
        /// 读取全部参数
        /// </summary>
        /// <param name="param"></param>
        private void btn_ReadParam_Click(object sender, EventArgs e)
        {
            readOrWrite = false;
            this.Enabled = false;
            this.label1.Visible = true;
            this.panel1.Visible = true;
            this.progressBar1.Visible = true;
            this.progressBar1.Value = 0;
            ReadScrewParams.Clear();
            DDKBatch.dicA1.Clear();
            this.backgroundWorker1.RunWorkerAsync();
        }
        /// <summary>
        /// 处理读到的ddk参数，绑定到控件&更新数据库
        /// </summary>
        private void ProcessDdkParam()
        {
            dgvParamName.Rows.Clear();
            dgvParameter.Rows.Clear();
            BLL.Bll.DeleteAllDDKParameter();
            ReadScrewParams.Sort((x, y) => x.taskId.CompareTo(y.taskId));
            for (int i = 0; i < ReadScrewParams.Count; i++)
            {
                BLL.Bll.AddDDKParameterData(ReadScrewParams[i]);
                dgvParamName.Rows.Add();
                dgvParamName.Rows[i].Cells["ParamIndexColumn"].Value = ReadScrewParams[i].taskId;
                dgvParamName.Rows[i].Cells["ParamNameColumn"].Value = ReadScrewParams[i].name;
                dgvParamName.Rows[i].Cells["ParamNameColumn"].ToolTipText = $"参数{ReadScrewParams[i].taskId}";
            }
            //默认选择第一个参数
            if (dgvParamName.Rows.Count > 0)
            {
                dgvParamName.Rows[0].Selected = true;
                DataGridViewCellEventArgs dataGridViewCellEventArgs = new DataGridViewCellEventArgs(1, 0);
                dgvParamName_CellContentClick(dgvParamName, dataGridViewCellEventArgs);
            }
        }

        /// <summary>
        /// 添加电批参数
        /// </summary>
        /// <param name="param"></param>
        private void AddScrewParameters(ScrewParam param)
        {
            ReadScrewParams.Add(param);
        }
        /// <summary>
        /// 初始化图片导航
        /// </summary>
        private void InitImageNavigation()
        {
            pic_TorqueParams.Image = GetThumbnail(torqueParmImg, 50, 50);
            pic_TorqueParams.Click += Pic_TorqueParams_Click;
            pic_TorqueRange.Image = GetThumbnail(torqueLimtParmImg, 50, 50);
            pic_TorqueRange.Click += Pic_TorqueRange_Click;
            pic_AngleSpeed.Image = GetThumbnail(speedParmImg, 50, 50);
            pic_AngleSpeed.Click += Pic_AngleSpeed_Click;
        }
        /// <summary>
        /// 扭矩参数设置示意图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Pic_TorqueParams_Click(object sender, EventArgs e)
        {
            pic_ParamSet.Image = Properties.Resources.parameterSet1;
            lblSet1.ForeColor = Color.Blue;
            lblSet2.ForeColor = Color.Black;
            lblSet3.ForeColor = Color.Black;
        }
        /// <summary>
        /// 扭矩范围参数设置示意图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Pic_TorqueRange_Click(object sender, EventArgs e)
        {
            pic_ParamSet.Image = Properties.Resources.parameterSet2;
            lblSet1.ForeColor = Color.Black;
            lblSet2.ForeColor = Color.Blue;
            lblSet3.ForeColor = Color.Black;
        }
        /// <summary>
        /// 速度角度参数设置示意图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Pic_AngleSpeed_Click(object sender, EventArgs e)
        {
            pic_ParamSet.Image = Properties.Resources.parameterSet3;
            lblSet1.ForeColor = Color.Black;
            lblSet2.ForeColor = Color.Black;
            lblSet3.ForeColor = Color.Blue;
        }

        /// <summary>
        /// 获取图片缩略图
        /// </summary>
        /// <param name="originalImage"></param>
        /// <param name="thumbWidth"></param>
        /// <param name="thumbHeight"></param>
        /// <returns></returns>
        public Image GetThumbnail(Image originalImage, int thumbWidth, int thumbHeight)
        {
            Image thumbnail = originalImage.GetThumbnailImage(thumbWidth, thumbHeight, () => { return false; }, IntPtr.Zero);
            return thumbnail;
        }
        /// <summary>
        /// 0读出，1写入
        /// </summary>
        bool readOrWrite = false;
        private void btn_WriteParam_Click(object sender, EventArgs e)
        {
            readOrWrite = true;
            this.Enabled = false;
            this.label1.Visible = true;
            this.panel1.Visible = true;
            this.progressBar1.Visible = true;
            this.progressBar1.Value = 0;
            this.backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            if (readOrWrite)
            {
                int index = (int)dgvParamName.Rows[dgvParamName.CurrentRow.Index].Cells["ParamIndexColumn"].Value;
                var writeParam = ReadScrewParams.Find(o => o.taskId == index);
                writeParam.name = writeParam.tighteningType == DDKBatch.TighteningTypeList[1] ? $"{writeParam.targeAngle}D" : $"{writeParam.targetTorque}N";
                dgvParamName.Rows[dgvParamName.CurrentRow.Index].Cells["ParamNameColumn"].Value = writeParam.name;

                DDKBatch.WriteDDK(writeParam);
                var present = 1;
                while (present < 50)
                {
                    Thread.Sleep(1);
                    this.backgroundWorker1.ReportProgress(present * 2);
                    present++;
                }
                BLL.Bll.UpdateDDKParameterData(writeParam);
            }
            else
            {
                for (int i = 0; i < 32; i++)
                {
                    DDKBatch.ReadScrewUpParameters(i + 1);
                    Thread.Sleep(50);
                    this.backgroundWorker1.ReportProgress(Convert.ToInt32(100.00 / 32 * (i + 1)));
                }
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            this.label1.Text = readOrWrite ? $"正在写入参数{e.ProgressPercentage}%" : $"正在读出参数{e.ProgressPercentage}%";
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!readOrWrite)
            {
                ProcessDdkParam();
            }
            this.Enabled = true;
            this.progressBar1.Visible = false;
            this.panel1.Visible = false;
            this.label1.Visible = false;
        }
    }
}
